package com.zmn.oms.dubbo.impl.zmn.normal.order;

import com.alibaba.fastjson.JSON;
import com.zmn.base.channel.common.dro.ChannelDRO;
import com.zmn.base.channel.dubbo.interfaces.channel.ChannelListRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.mcc.dubbo.interfaces.staff.StaffListRemoteService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.dubbo.dto.zmn.order.DmsOrderDRO;
import com.zmn.oms.dubbo.interfaces.zmn.normal.order.ZsNormalDmsOrderRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * 类描述： 分销支撑-工单
 *
 * @author heciqi
 * @date 2019/11/04 14:29
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 20000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ZsNormalDmsOrderRemoteServiceImpl implements ZsNormalDmsOrderRemoteService {
	static final String TAG = "普通单-分销支撑";

	@Autowired
	OrderWorkService orderWorkService;
	@Autowired
	OrderDetailService orderDetailService;
	@Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
	StaffListRemoteService staffListRemoteService;
	@Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
	ChannelListRemoteService channelListRemoteService;

	/**
	 * 获取订单信息
	 *
	 * @param orderId 订单id
	 * @param workId  工单id
	 * @return
	 */
	@Override
	public com.zmn.common.dto2.ResponseDTO<DmsOrderDRO> getOrderById(Long orderId, Long workId) {
		OrderWork orderWork = orderWorkService.findOrderWorkByKeySrcMaster(orderId, workId);
		if (orderWork == null) {
			return com.zmn.common.dto2.ResponseDTO.fail("订单不存在");
		}

		OrderDetail orderDetail = orderDetailService.findOrderDetailByKeySrcMaster(orderWork.getOrderId());
		DmsOrderDRO dmsOrderDRO = BeanMapper.map(orderDetail, DmsOrderDRO.class);// 映射详情工单
		BeanMapper.copy(orderWork, dmsOrderDRO);// 映射工单

		// 退款单处理
		if (Objects.equals(orderWork.getType(), OrderConsts.ORDER_TYPE_REFUND)) {
			OrderWork sourceOrderWork = orderWorkService.findOrderWorkByKeySrcMaster(orderWork.getOrderId(), orderWork.getOrderId());// 退款单原单id就是订单id
			if (sourceOrderWork != null && Objects.equals(sourceOrderWork.getType(), OrderConsts.ORDER_TYPE_REWORK)) {
				// 如果退款原单是返修单，得到该返修单对应的原单
				dmsOrderDRO.setOriginalOrderId(sourceOrderWork.getOriginalId());
				dmsOrderDRO.setOriginalReworkId(sourceOrderWork.getOriginalId());
			} else {
				dmsOrderDRO.setOriginalOrderId(orderWork.getOrderId());
				dmsOrderDRO.setOriginalReworkId(orderWork.getOrderId());
			}
		} else if (Objects.equals(orderWork.getType(), OrderConsts.ORDER_TYPE_REWORK)) {
			dmsOrderDRO.setOriginalOrderId(orderWork.getOriginalId());
			dmsOrderDRO.setOriginalReworkId(orderWork.getOriginalId());
		}

		// 查询原单渠道id
		if (!NumberUtil.isNullOrZero(dmsOrderDRO.getOriginalOrderId())) {
			OrderWork originalOrderWork = orderWorkService.findOrderWorkByKey(dmsOrderDRO.getOriginalOrderId(), dmsOrderDRO.getOriginalOrderId());
			if (originalOrderWork != null) {
				dmsOrderDRO.setOriginalChannelId(originalOrderWork.getChannelId());
				dmsOrderDRO.setOriginalChannelName(originalOrderWork.getChannelName());
				dmsOrderDRO.setOriginalUserId(originalOrderWork.getUserId());
				dmsOrderDRO.setOriginalVisitUserId(originalOrderWork.getVisitUserId());
			}
		}

		// 补充信息
		dmsOrderDRO.setCheckoutSuccess(Objects.equals(orderWork.getResultStatus(), OrderStatusConsts.WORK_RESULT_SUCCESS) ? GlobalConsts.YES : GlobalConsts.NO);// 成功失败状态

		if (!NumberUtil.isNullOrZero(dmsOrderDRO.getCheckouterId())) {// 收单员名称
			ResponseDTO<StaffDRO> staff = staffListRemoteService.getStaff(dmsOrderDRO.getCheckouterId().intValue());
			dmsOrderDRO.setCheckouterName(staff.getData() != null ? staff.getData().getRealName() : "");
			if (log.isDebugEnabled()) {
				log.debug("[{}]收单员[{}]", TAG, JSON.toJSONString(staff));
			}

		}

		if (!NumberUtil.isNullOrZero(dmsOrderDRO.getSourceChannelId())) {// 来源渠道名称
			com.zmn.common.dto2.ResponseDTO<ChannelDRO> channel = channelListRemoteService.getByChannelId(dmsOrderDRO.getSourceChannelId());
			dmsOrderDRO.setSourceChannelName(channel.getData() != null ? channel.getData().getName() : "");
			if (log.isDebugEnabled()) {
				log.debug("[{}]来源渠道[{}]", TAG, JSON.toJSONString(channel));
			}
		}

		log.debug("[{}]返回[{}]", TAG, dmsOrderDRO);
		return com.zmn.common.dto2.ResponseDTO.success(dmsOrderDRO);
	}
}
